<template>
    <view>
        <view class="sjinfonav">
            <scroll-view class="nav-two" :scrollX="true">
                <view
                    @tap="selectednavbar"
                    :class="'toatal_head  ' + (selectedindex == index ? 'selsect' : 'default')"
                    :data-index="index"
                    :style="'color:' + (selectedindex == index ? color : '')"
                    v-for="(item, index) in navbar"
                    :key="index"
                >
                    <view class="toataltime" v-if="index < 3">
                        <text>{{ item.name }}</text>
                        <text>{{ item.time }}</text>
                    </view>

                    <view class="toataltime" v-else>
                        <picker @change="bindDateChange" end="" mode="date" name="datepicker" :start="startdate" :value="startdate">
                            <view class="picker reserve_sele">{{ item.name }}</view>
                        </picker>
                    </view>

                    <view class="toatal_text" :style="'background-color: ' + color" v-if="selectedindex == index"></view>
                </view>
            </scroll-view>
        </view>
        <view class="yxrq">已选日期:{{ yxtime }}</view>
        <view class="timearr">
            <view
                @tap="selectedTime"
                class="timecell"
                :data-index="index"
                :style="'color:' + (selectedtime == index ? '#fff' : '') + ';background:' + (selectedtime == index ? color : '')"
                v-for="(item, index) in tiemarr"
                :key="index"
            >
                {{ item.time }}
            </view>
        </view>
        <form @reset="formReset" @submit="formSubmit" :reportSubmit="true">
            <view class="footer">
                <button formType="submit" :style="'background:' + color">确认选择</button>
            </view>
        </form>
    </view>
</template>

<script>
import cardlist from '@/ymq_canyino2o/dist/cardlist/index';
import gettel from '@/ymq_canyino2o/dist/gettel/index';
var app = getApp();
var util = require('../../utils/util.js');
export default {
    components: {
        cardlist,
        gettel
    },
    data() {
        return {
            navbar: [],
            selectedindex: 0,
            tiemarr: '',
            yxtime: '',
            startdate: '',
            selectedtime: '',
            date: '',
            time: '',
            color: ''
        };
    },
    onLoad: function (e) {
        function t(e) {
            var t = new Date();
            t.setDate(t.getDate() + e);
            var a = t.getFullYear();
            var n = t.getMonth() + 1;
            if (n < 10) {
                n = '0' + n;
            }
            var i = t.getDate();
            if (i < 10) {
                i = '0' + i;
            }
            t.getHours();
            t.getMinutes();
            t.getSeconds();
            return a + '-' + n + '-' + i;
        }
        uni.setNavigationBarTitle({
            title: '选择时间'
        });
        app.globalData.setNavigationBarColor(this);
        var that = this;
        var n = util.formatTime(new Date()).substring(0, 10).replace(/\//g, '-');
        console.log(e, t(0), t(1), t(2));
        app.globalData.util.request({
            url: 'entry/wxapp/GetStoreTime',
            cachetime: '0',
            data: {
                store_id: e.storeid
            },
            success: function (e) {
                console.log(e);
                that.setData({
                    tiemarr: e.data,
                    yxtime: t(0),
                    startdate: n
                });
            }
        });
        app.globalData.util.request({
            url: 'entry/wxapp/GetYdSet',
            cachetime: '0',
            data: {
                store_id: e.storeid
            },
            success: function (e) {
                console.log(e);
                if ('1' == e.data.is_ydtime) {
                    that.setData({
                        navbar: [
                            {
                                name: '今天',
                                time: t(0)
                            }
                        ]
                    });
                } else {
                    that.setData({
                        navbar: [
                            {
                                name: '今天',
                                time: t(0)
                            },
                            {
                                name: '明天',
                                time: t(1)
                            },
                            {
                                name: '后天',
                                time: t(2)
                            },
                            {
                                name: '其他时间'
                            }
                        ]
                    });
                }
            }
        });
    },
    onReady: function () {},
    onShow: function () {},
    onHide: function () {},
    onUnload: function () {},
    onPullDownRefresh: function () {},
    onReachBottom: function () {},
    onShareAppMessage: function () {},
    methods: {
        selectednavbar: function (e) {
            console.log(e);
            this.setData({
                selectedindex: e.currentTarget.dataset.index,
                yxtime: this.navbar[e.currentTarget.dataset.index].time
            });
        },

        selectedTime: function (e) {
            console.log(e);
            this.setData({
                selectedtime: e.currentTarget.dataset.index
            });
        },

        bindDateChange: function (e) {
            console.log('picker发送选择改变，携带值为', e.detail.value);
            this.setData({
                yxtime: e.detail.value
            });
        },

        formSubmit: function (e) {
            var t = getCurrentPages();
            var that = this;
            var n = uni.getStorageSync('users').id;
            app.globalData.util.request({
                url: 'entry/wxapp/AddFormId',
                cachetime: '0',
                data: {
                    user_id: n,
                    form_id: e.detail.formId
                },
                success: function (e) {
                    console.log(e.data);
                }
            });
            if (null != this.selectedtime) {
                if (1 < t.length) {
                    t[t.length - 2].setData({
                        date: that.yxtime,
                        time: that.tiemarr[that.selectedtime].time
                    });
                }
                setTimeout(function () {
                    uni.navigateBack({});
                }, 500);
            } else {
                uni.showModal({
                    title: '提示',
                    content: '请选择时间'
                });
            }
        },

        formReset() {
            console.log('占位：函数 formReset 未声明');
        }
    }
};
</script>
<style>
@import './reservetime.css';
</style>
